package com.pedro.streamer;

import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.VideoView;

import androidx.constraintlayout.widget.ConstraintLayout;

import java.util.ArrayList;

public class VideoPlayMainActivity extends BaseMainActivity implements View.OnClickListener {
    private String TAG = "com.bmi.vcam";

    private VideoView videoView;
    private TextView backBtn,durText,totalText;
    private ConstraintLayout constraintLayout;

    private TextView btn_start;

    private SeekBar seekBar;

    private ArrayList<String> pathArr;

    private String curPath;
    private  int count;

    final Handler handler = new Handler();
    final Runnable updateProgress = new Runnable() {
        @Override
        public void run() {
            int currentPosition = videoView.getCurrentPosition();
            int maxDuration = videoView.getDuration();
            if (maxDuration > 0) {
                seekBar.setProgress(currentPosition);
                seekBar.setMax(maxDuration);
            }


            durText.setText(convertMillisToDuration(currentPosition));
            handler.postDelayed(this, 100);
        }
    };
    public static String convertMillisToDuration(long millis) {
        long seconds = millis / 1000 % 60;
        long minutes = millis / (1000 * 60) % 60;
        long hours = millis / (1000 * 60 * 60);

        return String.format("%02d:%02d:%02d", hours, minutes, seconds);
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video_play_main);

        bindViews();
    }


    private void bindViews() {
        videoView = (VideoView) findViewById(R.id.videoView);


        btn_start = (TextView) findViewById(R.id.btn_start);

        backBtn = (TextView) findViewById(R.id.close_btn);
        seekBar = findViewById(R.id.seek_bar);

        durText = findViewById(R.id.during_time);
        totalText = findViewById(R.id.total_time);
        constraintLayout = findViewById(R.id.bottom_layout);
        constraintLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

            }
        });

        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                if (fromUser) {
                    // 用户拖动进度条时，跳转到指定位置
                    videoView.seekTo(progress);
                }
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                // 用户开始拖动进度条
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                // 用户停止拖动进度条
            }
        });

        Intent intent = getIntent();

        if (intent.hasExtra("VideoPath")) {

            curPath = (String) intent.getExtras().get("VideoPath");
            pathArr = (ArrayList<String>) intent.getExtras().get("VideoArr");
            for (int i = 0; i < pathArr.size(); i++) {
                String path = pathArr.get(i);
                if (path.equals(curPath)){
                    count = i;
                }

            }
            curPath = pathArr.get(count);
            videoView.setVideoPath(curPath);
            videoView.setFocusable(true);
            videoView.start();
            handler.postDelayed(updateProgress, 0);

        }

        videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                // 视频播放结束时的回调
                // 在这里编写你需要执行的代码

                count++;
                if (count>=pathArr.size()){
                    count = 0;
                    playFinish();
                }else {
                    curPath = pathArr.get(count);
                    videoView.setVideoPath(curPath);
                    videoView.setFocusable(true);
                    videoView.start();
                    handler.postDelayed(updateProgress, 0);
                }


            }
        });

        videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mediaPlayer) {
                totalText.setText(convertMillisToDuration(videoView.getDuration()));
            }
        });

        videoView.setOnErrorListener(new MediaPlayer.OnErrorListener() {
            @Override
            public boolean onError(MediaPlayer mediaPlayer, int i, int i1) {
                playFinish();
                return false;
            }
        });
        btn_start.setOnClickListener(this);
        backBtn.setOnClickListener(this);
        videoView.setOnClickListener(this);


    }


    private void playFinish() {
        btn_start.setBackground(getDrawable(R.mipmap.usb_main_ic_player_play_n));
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.videoView || id == R.id.btn_start) {
            Log.d(TAG, "onClick: ");
            if (videoView.isPlaying()) {
                btn_start.setBackground(getDrawable(R.mipmap.usb_main_ic_player_play_n));
                videoView.pause();
            } else {
                if (curPath.equals(pathArr.get(count))) {
                    videoView.start();
                } else {
                    curPath = pathArr.get(count);
                    videoView.setVideoPath(curPath);
                    videoView.setFocusable(true);
                    videoView.start();
                    handler.postDelayed(updateProgress, 0);
                }
                btn_start.setBackground(getDrawable(R.mipmap.usb_main_ic_player_pause_n));

            }
        } else if (id == R.id.close_btn) {
            finish();
        }
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();

    }
}